var flag=1;
var gcid=getStorage("smallCategoryId");
$(document).ready(function () {
    handle();
    showList();
});
//基本操作
function handle(){
    //新增
    $("#category").click(function () {
        edit();
    })
}
function edit(){
    parent.layer.open({
        type: 2,
        title: '编辑信息',
        maxmin: true,
        shadeClose: true, //点击遮罩关闭层
        area : ['60%', '80%'],
        content: 'goodsSmallCategoryModify.html',
        end: function(index, layero){
            delete window.localStorage.smallMsg;
            showList();
        }
    });
}

function showList(){
    if(flag!==1){
        return
    }
    flag=0;
    ajax({
        url:"category/goodsSubclass",
        data:{
            gcid:gcid
        },
        success:function(data){
            
            flag=1;
            if(data.success){
                document.getElementById("tbody").innerHTML=template("template",data);
                $("#totalPage").html(data.data.length);
            }else{
                layer.msg(data.message);
            }
        }
    });
}

$("#tbody").on("click",".edit",function () {
    var categoryMsg=$(this.parentNode.parentNode.parentNode).attr("data-msg");
    setStorage("smallMsg",categoryMsg);
    edit();
}).on("click",".delete",function () {
    var categoryMsg=$(this.parentNode.parentNode.parentNode).attr("data-msg");
    var msg=JSON.parse(categoryMsg);
    var gsid=msg.gsid;
    parent.layer.confirm('确定要删除吗？', {
        btn: ['确定','取消'] //按钮
    }, function(index){
        deleteItem(gsid);
        parent.layer.close(index);
    }, function(){
    });
}).on("blur",".sort",function () {
    var categoryMsg=$(this.parentNode.parentNode).attr("data-msg");
    var msg=JSON.parse(categoryMsg);
    var gsid=msg.gsid;
    var gsSort=$(this).val();
    if (parseFloat(gsSort).toString() === "NaN") {
        layer.msg("请输入正确数字");
    } else {
        sort(gsid,gsSort);
    }
});
//排序
function sort(gsid,gsSort){
    if(flag!==1){
        return
    }
    flag=0;
    ajax({
        url:"category/updateSort",
        data:{
            gsid:gsid,
            gsSort:gsSort
        },
        success:function(data){
            
            flag=1;
            layer.msg(data.message);
            if(data.success){
                showList();
            }
        }
    });
}
//删除
function deleteItem(gsid){
    if(flag!==1){
        return
    }
    flag=0;
    ajax({
        url:"category/deleteCategory",
        data:{
            gsid:gsid
        },
        success:function(data){
            
            flag=1;
            if(data.success){
                showList();
            }else{
                layer.msg(data.message);
            }
        }
    });
}